package pl.diagnoser.client;

import java.util.ArrayList;
import java.util.List;
import pl.diagnoser.client.formulas.DiagnoserHBFormula;
import pl.diagnoser.client.formulas.DiagnoserIFormula;
import pl.diagnoser.client.formulas.DiagnoserSFormula;
import pl.diagnoser.client.formulas.DiagnoserVFormula;
import pl.diagnoser.client.tools.DataGetterService;
import pl.diagnoser.client.tools.DataGetterServiceAsync;
import pl.diagnoser.client.tools.DiagnoserProperties;
import pl.diagnoser.client.tools.LogIn;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.RowEndEditAction;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.EditCompleteEvent;
import com.smartgwt.client.widgets.grid.events.EditCompleteHandler;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;

public class PrzegladanieWiedzy extends Canvas {
	
	private static DataGetterServiceAsync dataGetterAsc = GWT.create(DataGetterService.class);
	private DiagnoserProperties properties = new DiagnoserProperties();
	private TabSet tabSet = new TabSet();
	private Tab defectsAndSymptoms = new Tab();
	private Tab hRulesFromKnowledgeBase = new Tab();
	private Tab vRulesFromKnowledgeBase = new Tab();
	private Tab sRulesFromKnowledgeBase = new Tab();
	private Tab iRulesFromKnowledgeBase = new Tab();
	
	private ListGrid hierachyGrid = new ListGrid();
	private ListGrid statementGrid = new ListGrid();
	private ListGrid similarityGrid = new ListGrid();
	private ListGrid implicationGrid = new ListGrid();
	
	PrzegladanieWiedzy() {
		this.setHeight100();
		this.setWidth100();
		
		
		defectsAndSymptoms.setTitle("Słowniki");
		defectsAndSymptoms.setPane(defectsAndSymptomsLayout());
		
		hRulesFromKnowledgeBase.setTitle("Hierarchie");
		hRulesFromKnowledgeBase.setPane(hierachyLayout());
		
		vRulesFromKnowledgeBase.setTitle("Stwierdzenia");
		vRulesFromKnowledgeBase.setPane(statementLayout());
		
		sRulesFromKnowledgeBase.setTitle("Podobieństwa");
		sRulesFromKnowledgeBase.setPane(similarityLayout());
		
		iRulesFromKnowledgeBase.setTitle("Implikacje");
		iRulesFromKnowledgeBase.setPane(implicationLayout());
		
		tabSet.setWidth100();
		tabSet.setHeight100();
		tabSet.addTabSelectedHandler(new TabSelectedHandler() {
			public void onTabSelected(TabSelectedEvent event) {
				switch(event.getTabNum()) {
					case 1: loadHBFormulas(hierachyGrid);
							break;
					case 2: loadVFormulas(statementGrid);
							break;
					case 3: loadSFormulas(similarityGrid);
							break;
					case 4: loadIFormulas(implicationGrid);
							break;
				}
			}
		});
		tabSet.setTabs(defectsAndSymptoms, hRulesFromKnowledgeBase,
				vRulesFromKnowledgeBase, sRulesFromKnowledgeBase,
				iRulesFromKnowledgeBase);
		
		this.addChild(tabSet);
		this.draw();
		
	}
	
	private Canvas defectsAndSymptomsLayout() {
		
		final Canvas mainLayout = new Canvas();
		
		//Atrybuty
		final ListGridField attributeName = new ListGridField("attributeName", "Atrybuty");
		attributeName.setType(ListGridFieldType.TEXT);
		attributeName.setRequired(true);
		
		final ListGridField isDate = new ListGridField("isDate","Data");
		isDate.setType(ListGridFieldType.BOOLEAN);
		
		final ListGridField isMultiple = new ListGridField("isMultiple","Wielokrotny");
		isMultiple.setType(ListGridFieldType.BOOLEAN);
		
		final ListGridField isFile = new ListGridField("isFile","Plik");
		isFile.setType(ListGridFieldType.BOOLEAN);
		
		final ListGrid attributesGrid = new ListGrid();
		attributesGrid.setWidth(350);
		attributesGrid.setHeight(350);
		attributesGrid.setShowAllRecords(true);
		attributesGrid.setSelectionType(SelectionStyle.SINGLE);
		attributesGrid.setFields(attributeName, isDate, isMultiple, isFile);
		attributesGrid.addEditCompleteHandler(new EditCompleteHandler() {
			public void onEditComplete(EditCompleteEvent event) {
				if(event.getOldRecord() == null) {
					ListGridRecord newRecord = (ListGridRecord) attributesGrid.getRecord(event.getRowNum());
					Dictionary.getInstance().save(newRecord,"ATTRIBUTE",null,attributesGrid);
				}
			}
		});
		Dictionary.getInstance().loadAttributesToGrid(attributesGrid);
		
		final Button newAttribute = new Button("Dodaj");
		newAttribute.setHeight(18);
		newAttribute.setWidth(90);
		newAttribute.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				attributesGrid.startEditingNew();
			}
		});
		
		final Button removeAttribute = new Button("Usuń");
		removeAttribute.setHeight(18);
		removeAttribute.setWidth(90);
		removeAttribute.disable();
		removeAttribute.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Dictionary.getInstance().remove(attributesGrid.getSelectedRecords(),"ATTRIBUTE",null,attributesGrid);
				attributesGrid.removeSelectedData();
			}
		});
		
		final HLayout attributesButtonLayout = new HLayout();
		attributesButtonLayout.setWidth(245);
		attributesButtonLayout.setHeight(10);
		attributesButtonLayout.setAlign(Alignment.CENTER);
		attributesButtonLayout.setMembersMargin(10);
		attributesButtonLayout.addMember(newAttribute);
		attributesButtonLayout.addMember(removeAttribute);
		
		final VLayout attributesLayout = new VLayout();
		attributesLayout.setMembersMargin(10);
		attributesLayout.addMember(attributesButtonLayout);
		attributesLayout.addMember(attributesGrid);
		//KONIEC atrybutow
		
		
		if(LogIn.getInstance().getUser().getAppId() != LogIn.SEARCH_APP) {
			//DEFEKTY
			final ListGridField defectName = new ListGridField("name", "defectsGrid.name");
	        defectName.setType(ListGridFieldType.TEXT);
	        defectName.setRequired(true);
	        
	        final Button removeDefect = new Button("removeDictDefect");
	        
	        final ListGrid defectsGrid = new ListGrid();
	        defectsGrid.setWidth(400);
	        defectsGrid.setHeight(350);
	        defectsGrid.setShowAllRecords(true);
	        defectsGrid.setSelectionType(SelectionStyle.SIMPLE);
	        defectsGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
	        defectsGrid.setFields(defectName);
	        defectsGrid.setEditEvent(ListGridEditEvent.CLICK);
	        defectsGrid.setListEndEditAction(RowEndEditAction.NEXT);
	        defectsGrid.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					removeDefect.enable();
				}
			});
	        defectsGrid.addEditCompleteHandler(new EditCompleteHandler() {
	                public void onEditComplete(EditCompleteEvent event) {
	                        if(event.getOldRecord() == null) {
	                                ListGridRecord newRecord = (ListGridRecord) defectsGrid.getRecord(event.getRowNum());
	                                Dictionary.getInstance().save(newRecord,"DEFECT",null,defectsGrid);
	                        }
	                }
	        });
	        Dictionary.getInstance().loadDefectsToGrid(defectsGrid);
	        properties.getListGridTitle("name", "defectsGrid", defectsGrid, false);
	        
	        final Button newDefect = new Button("newDictDefect");
	        newDefect.setHeight(18);
	        newDefect.setWidth(90);
	        newDefect.addClickHandler(new ClickHandler() {
	                public void onClick(ClickEvent event) {
	                        defectsGrid.startEditingNew();
	                }
	        });
	        properties.getWidgetTitle("newDictDefect", newDefect);
	        
	        removeDefect.setHeight(18);
	        removeDefect.setWidth(90);
	        removeDefect.addClickHandler(new ClickHandler() {
	                public void onClick(ClickEvent event) {
	                        Dictionary.getInstance().remove(defectsGrid.getSelectedRecords(),"DEFECT",null,defectsGrid);
	                        defectsGrid.removeSelectedData();
	                }
	        });
	        properties.getWidgetTitle("removeDictDefect", removeDefect);
	        
	        final HLayout defectsButtonLayout = new HLayout();
	        defectsButtonLayout.setWidth(245);
	        defectsButtonLayout.setHeight(10);
	        defectsButtonLayout.setAlign(Alignment.CENTER);
	        defectsButtonLayout.setMembersMargin(10);
	        defectsButtonLayout.addMember(newDefect);
	        defectsButtonLayout.addMember(removeDefect);
			
	        attributesLayout.addMember(defectsButtonLayout);
			attributesLayout.addMember(defectsGrid);
		}
		
		///Wartości atrybutow
		final ListGridField attributeValueName = new ListGridField("name", "Wartości");
		attributeValueName.setType(ListGridFieldType.TEXT);
		
		final Button removeAttributeValue = new Button("Usuń");
		
		final ListGrid attributesValuesGrid = new ListGrid();
		attributesValuesGrid.setWidth(400);
		attributesValuesGrid.setHeight(350);
		attributesValuesGrid.setShowAllRecords(true);
		attributesValuesGrid.setSelectionType(SelectionStyle.SINGLE);
		attributesValuesGrid.setEditEvent(ListGridEditEvent.CLICK);
		attributesValuesGrid.setListEndEditAction(RowEndEditAction.NEXT);
		attributesValuesGrid.setFields(attributeValueName);
		attributesValuesGrid.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				removeAttributeValue.enable();
			}
		});
		attributesValuesGrid.addEditCompleteHandler(new EditCompleteHandler() {
			public void onEditComplete(EditCompleteEvent event) {
				if(event.getOldRecord() == null) {
					ListGridRecord newRecord = (ListGridRecord) attributesValuesGrid.getRecord(event.getRowNum());
					Dictionary.getInstance().save(newRecord,"ATTRIBUTE_VALUE",attributesGrid.getSelectedRecord().getAttributeAsString("attributeName"),attributesValuesGrid);
				}
			}
		});
		
		final Button newAttributeValue = new Button("Dodaj");
		newAttributeValue.setHeight(18);
		newAttributeValue.setWidth(90);
		newAttributeValue.disable();
		newAttributeValue.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				attributesValuesGrid.startEditingNew();
			}
		});
		
		removeAttributeValue.setHeight(18);
		removeAttributeValue.setWidth(90);
		removeAttributeValue.disable();
		removeAttributeValue.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Dictionary.getInstance().remove(attributesValuesGrid.getSelectedRecords(),"ATTRIBUTE_VALUE",attributesGrid.getSelectedRecord().getAttributeAsString("attributeName"),attributesValuesGrid);
				attributesValuesGrid.removeSelectedData();
			}
		});
		
		final HLayout attributeValuesButtonLayout = new HLayout();
		attributeValuesButtonLayout.setWidth(245);
		attributeValuesButtonLayout.setHeight(10);
		attributeValuesButtonLayout.setAlign(Alignment.CENTER);
		attributeValuesButtonLayout.setMembersMargin(10);
		attributeValuesButtonLayout.addMember(newAttributeValue);
		attributeValuesButtonLayout.addMember(removeAttributeValue);
		
		final VLayout attributeValuesLayout = new VLayout();
		attributeValuesLayout.setMembersMargin(10);
		attributeValuesLayout.addMember(attributeValuesButtonLayout);
		attributeValuesLayout.addMember(attributesValuesGrid);
		
		
		attributesGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
			public void onSelectionChanged(SelectionEvent event) {
				if(attributesGrid.getSelectedRecord() != null) {
					if(!attributesGrid.getSelectedRecord().getAttributeAsBoolean("isDate")) {
						newAttributeValue.enable();
						/*
						if(DictionaryData.getInstance().getAttributeValuesRecordsSize(event.getSelectedRecord().getAttributeAsString("attributeName")) > 0)
							removeAttributeValue.enable();
						else
							removeAttributeValue.disable();
							*/
					} else {
						newAttributeValue.disable();
						removeAttributeValue.disable();
					}
					removeAttribute.enable();
					Dictionary.getInstance().loadSelectedAttributeValues(event.getSelectedRecord().getAttributeAsString("attributeName").replace(" ", "_"), attributesValuesGrid);
				}
			}
		});
		//KONIEC wartosci atrybutow
		
		
		if(LogIn.getInstance().getUser().getAppId() != LogIn.SEARCH_APP) {
			//SYMPTOMY
			final ListGridField symptomName = new ListGridField("name", "symptomsGrid.name");
	        symptomName.setType(ListGridFieldType.TEXT);
	        
	        final Button removeSymptom = new Button("removeDictSymptom");
	        
	        final ListGrid symptomsGrid = new ListGrid();
	        symptomsGrid.setWidth(400);
	        symptomsGrid.setHeight(350);
	        symptomsGrid.setShowAllRecords(true);
	        symptomsGrid.setSelectionType(SelectionStyle.SIMPLE);
	        symptomsGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
	        symptomsGrid.setFields(symptomName);
	        symptomsGrid.setEditEvent(ListGridEditEvent.CLICK);
	        symptomsGrid.setListEndEditAction(RowEndEditAction.NEXT);
	        symptomsGrid.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					removeSymptom.enable();
				}
			});
	        symptomsGrid.addEditCompleteHandler(new EditCompleteHandler() {
	                public void onEditComplete(EditCompleteEvent event) {
	                        if(event.getOldRecord() == null) {
	                                ListGridRecord newRecord = (ListGridRecord) symptomsGrid.getRecord(event.getRowNum());
	                                Dictionary.getInstance().save(newRecord,"SYMPTOM",null,symptomsGrid);
	                        }
	                }
	        });
	        Dictionary.getInstance().loadSymptomsToGrid(symptomsGrid);
	        properties.getListGridTitle("name", "symptomsGrid", symptomsGrid, false);
	        
	        final Button newSymptom = new Button("newDictSymptom");
	        newSymptom.setHeight(18);
	        newSymptom.setWidth(90);
	        newSymptom.addClickHandler(new ClickHandler() {
	                public void onClick(ClickEvent event) {
	                        symptomsGrid.startEditingNew();
	                }
	        });
	        
	        properties.getWidgetTitle("newDictSymptom", newSymptom);
	        
	        removeSymptom.setHeight(18);
	        removeSymptom.setWidth(90);
	        removeSymptom.addClickHandler(new ClickHandler() {
	                public void onClick(ClickEvent event) {
	                        Dictionary.getInstance().remove(symptomsGrid.getSelectedRecords(),"SYMPTOM",null,symptomsGrid);
	                        symptomsGrid.removeSelectedData();
	                }
	        });
	        properties.getWidgetTitle("removeDictSymptom", removeSymptom);
	        
	        final HLayout symptomsButtonLayout = new HLayout();
	        symptomsButtonLayout.setWidth(245);
	        symptomsButtonLayout.setHeight(10);
	        symptomsButtonLayout.setAlign(Alignment.CENTER);
	        symptomsButtonLayout.setMembersMargin(10);
	        symptomsButtonLayout.addMember(newSymptom);
	        symptomsButtonLayout.addMember(removeSymptom);
			
	        attributeValuesLayout.addMember(symptomsButtonLayout);
	        attributeValuesLayout.addMember(symptomsGrid);
		}
        
		final HLayout defectsAndSymptomsLayout = new HLayout();
		defectsAndSymptomsLayout.setTop(10);
		defectsAndSymptomsLayout.setLeft(10);
		defectsAndSymptomsLayout.setMembersMargin(5);
		defectsAndSymptomsLayout.addMember(attributesLayout);
		defectsAndSymptomsLayout.addMember(attributeValuesLayout);
		
		mainLayout.addChild(defectsAndSymptomsLayout);
		return mainLayout;
	}
	
	private Canvas hierachyLayout() {
		Canvas mainLayout = new Canvas();
		
		final ListGridField type = new ListGridField("type", "Rodzaj");
		final ListGridField object1 = new ListGridField("object1", "Obiekt 1");
		final ListGridField object2 = new ListGridField("object2", "Obiekt 2");
		final ListGridField context = new ListGridField("context", "Kontekst");
		final ListGridField typicality = new ListGridField("typicality", "Typowość");
		final ListGridField domination = new ListGridField("domination", "Dominacja");
		final ListGridField confidence = new ListGridField("confidence", "Pewność");
		
		hierachyGrid.setWidth100();
		hierachyGrid.setHeight100();
		hierachyGrid.setShowAllRecords(true);
		hierachyGrid.setFields(type, object1, object2, context, typicality, domination, confidence);
		
		mainLayout.addChild(hierachyGrid);
		return mainLayout;
	}
	
	private Canvas statementLayout() {
		Canvas mainLayout = new Canvas();
		
		final ListGridField object = new ListGridField("object", "Obiekt");
		final ListGridField attribute = new ListGridField("attribute", "Atrybut");
		final ListGridField value = new ListGridField("value", "Wartość");
		final ListGridField confidence = new ListGridField("confidence", "Pewność");
		
		statementGrid.setWidth100();
		statementGrid.setHeight100();
		statementGrid.setShowAllRecords(true);
		statementGrid.setFields(object, attribute, value, confidence);
		
		mainLayout.addChild(statementGrid);
		return mainLayout;
	}
	
	private Canvas similarityLayout() {
		Canvas mainLayout = new Canvas();
		
		final ListGridField object1 = new ListGridField("object1", "Obiekt 1");
		final ListGridField object2 = new ListGridField("object2", "Obiekt 2");
		final ListGridField context = new ListGridField("context", "Kontekst");
		final ListGridField rate = new ListGridField("rate", "Stopień podobieństwa");
		
		similarityGrid.setWidth100();
		similarityGrid.setHeight100();
		similarityGrid.setShowAllRecords(true);
		similarityGrid.setFields(object1, object2, context, rate);
		
		mainLayout.addChild(similarityGrid);
		return mainLayout;
	}
	
	private Canvas implicationLayout() {
		Canvas mainLayout = new Canvas();
		
		final ListGridField premises = new ListGridField("premises", "Przesłanki");
		final ListGridField conclusion = new ListGridField("conclusion", "Konkluzja");
		final ListGridField strength = new ListGridField("strength", "Siła");
		
		implicationGrid.setWidth100();
		implicationGrid.setHeight100();
		implicationGrid.setShowAllRecords(true);
		implicationGrid.setFields(premises, conclusion, strength);
		
		mainLayout.addChild(implicationGrid);
		return mainLayout;
	}
	
	private void loadHBFormulas(final ListGrid listGrid) {
		if(listGrid.getRecords().length > 0)
			return;
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<List<DiagnoserHBFormula>> callback = new AsyncCallback<List<DiagnoserHBFormula>>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(List<DiagnoserHBFormula> result) {
				List<ListGridRecord> records = FormulasData.getInstance().loadFormulasHB(result);
				listGrid.setData(records.toArray(new ListGridRecord[records.size()]));
			}
		};
		dataGetterAsc.getHBFormulas(callback);
	}
	
	private void loadVFormulas(final ListGrid listGrid) {
		if(listGrid.getRecords().length > 0)
			return;
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<List<DiagnoserVFormula>> callback = new AsyncCallback<List<DiagnoserVFormula>>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(List<DiagnoserVFormula> result) {
				List<ListGridRecord> records = FormulasData.getInstance().loadFormulasV(result);
				listGrid.setData(records.toArray(new ListGridRecord[records.size()]));
			}
		};
		dataGetterAsc.getVFormulas(callback);
	}
	
	private void loadSFormulas(final ListGrid listGrid) {
		if(listGrid.getRecords().length > 0)
			return;
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<List<DiagnoserSFormula>> callback = new AsyncCallback<List<DiagnoserSFormula>>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(List<DiagnoserSFormula> result) {
				List<ListGridRecord> records = FormulasData.getInstance().loadFormulasS(result);
				listGrid.setData(records.toArray(new ListGridRecord[records.size()]));
			}
		};
		dataGetterAsc.getSFormulas(callback);
	}
	
	private void loadIFormulas(final ListGrid listGrid) {
		if(listGrid.getRecords().length > 0)
			return;
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<List<DiagnoserIFormula>> callback = new AsyncCallback<List<DiagnoserIFormula>>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(List<DiagnoserIFormula> result) {
				List<ListGridRecord> records = FormulasData.getInstance().loadFormulasI(result);
				listGrid.setData(records.toArray(new ListGridRecord[records.size()]));
			}
		};
		dataGetterAsc.getIFormulas(callback);
	}
}

class FormulasData {
	
	public static FormulasData instance;
	public List<ListGridRecord> formulasRecord;
	
	public static FormulasData getInstance() {
		if(instance == null)
			return new FormulasData();
		return instance;
	}
	
	public List<ListGridRecord> loadFormulasHB(List<DiagnoserHBFormula> formulas) {
		List<ListGridRecord> records = new ArrayList<ListGridRecord>();
		for(DiagnoserHBFormula formula : formulas) {
			ListGridRecord tmpRecord = new ListGridRecord();
			tmpRecord.setAttribute("type", formula.getType());
			tmpRecord.setAttribute("object1", formula.getObject1());
			tmpRecord.setAttribute("object2", formula.getObject2());
			tmpRecord.setAttribute("context", formula.getContext());
			tmpRecord.setAttribute("typicality", formula.getTypicality());
			tmpRecord.setAttribute("domination", formula.getDomination());
			tmpRecord.setAttribute("confidence", formula.getConfidence());
			records.add(tmpRecord);
		}
		return records;
	}
	
	public List<ListGridRecord> loadFormulasV(List<DiagnoserVFormula> formulas) {
		List<ListGridRecord> records = new ArrayList<ListGridRecord>();
		for(DiagnoserVFormula formula : formulas) {
			ListGridRecord tmpRecord = new ListGridRecord();
			tmpRecord.setAttribute("object", formula.getObject());
			tmpRecord.setAttribute("attribute", formula.getAttribute());
			tmpRecord.setAttribute("value", formula.getValue());
			tmpRecord.setAttribute("confidence", formula.getConfidence());
			records.add(tmpRecord);
		}
		return records;
	}
	
	public List<ListGridRecord> loadFormulasS(List<DiagnoserSFormula> formulas) {
		List<ListGridRecord> records = new ArrayList<ListGridRecord>();
		for(DiagnoserSFormula formula : formulas) {
			ListGridRecord tmpRecord = new ListGridRecord();
			tmpRecord.setAttribute("object1", formula.getObject1());
			tmpRecord.setAttribute("object2", formula.getObject2());
			tmpRecord.setAttribute("context", formula.getContext());
			tmpRecord.setAttribute("rate", formula.getRate());
			records.add(tmpRecord);
		}
		return records;
	}
	
	public List<ListGridRecord> loadFormulasI(List<DiagnoserIFormula> formulas) {
		List<ListGridRecord> records = new ArrayList<ListGridRecord>();
		for(DiagnoserIFormula formula : formulas) {
			String premises = "";
			for(String premise : formula.getPremises()) {
				premises += premise + "\n";
			}
			ListGridRecord tmpRecord = new ListGridRecord();
			tmpRecord.setAttribute("premises", premises);
			tmpRecord.setAttribute("conclusion", formula.getConclusion());
			tmpRecord.setAttribute("strength", formula.getStrength());
			records.add(tmpRecord);
		}
		return records;
	}
}